package com.ruoyi.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

@Component
public class RestTemplateClient {

    private static RestTemplate restTemplate;
    public RestTemplateClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public static  <T> T get(Class<T> resultClass, String url, Map<String, Object> queryParams) {
        if (queryParams != null && queryParams.size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            boolean first = true;
            for (String key : queryParams.keySet()) {
                Object value = queryParams.get(key);
                if (value != null && StringUtils.isNotBlank(value.toString())) {
                    if (first) {
                        stringBuilder.append(key).append('=').append(value);
                    } else {
                        stringBuilder.append('&').append(key).append('=').append(value);
                    }
                    first = false;

                }
            }
            if (stringBuilder.length() > 0) {
                url = url + '?' + stringBuilder.toString();
            }
        }
        try {
            System.out.println("url=" + url);
            return restTemplate.getForObject(url, resultClass);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(String.format("调用远程[%s]服务错误", url));
        }
    }

    public static <T> ResponseEntity<T> getWithBody(String path, String jsonParam, Class<T> responseType) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonParam, headers);
        try {
            return restTemplate.exchange(path, HttpMethod.GET, httpEntity, responseType);
        } catch (Exception e) {
            throw new RuntimeException(String.format("调用远程[%s]服务错误", path), e);
        }
    }

    public static  <T> ResponseEntity<T> postWithBody(HttpHeaders headers, String path, String json, Class<T> resultClass) {
        RestTemplate restTemplate = new RestTemplate();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity<String>(json, headers);
        try {
            return restTemplate.exchange(path, HttpMethod.POST, entity, resultClass);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(String.format("调用远程[%s]服务错误", path), e);
        }
    }


}
